package cn.itcast.service.impl;

import cn.hutool.core.date.DateUtil;
import cn.itcast.dao.OrdersettingDao;
import cn.itcast.pojo.OrderSetting;
import cn.itcast.vo.OrderSettingVo;
import cn.itcast.service.OrdersettingService;
import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class OrdersettingServiceImpl implements OrdersettingService {

    @Autowired
    private OrdersettingDao ordersettingDao;

    //根据月份查询每日预约人数
    @Override
    public List<OrderSettingVo> findAll(String date) {
        List<OrderSetting> orderSetting = ordersettingDao.findAll(date);
        return orderSetting.stream().map(OrderSettingVo::init).collect(Collectors.toList());
    }

    //设置单日预约人数
    @Override
    public Integer upDate(OrderSetting orderSetting) {
        String date = DateUtil.format(orderSetting.getOrderDate(), "yyyy-MM-dd");
        int number = orderSetting.getNumber();
        Integer count = ordersettingDao.findByDate(date);
        if (count>0){
            return ordersettingDao.update(date, number);
        }else {
        return   ordersettingDao.save(orderSetting);
        }

    }

    @Override
    public void save(List<OrderSetting> list) {
        for (OrderSetting orderSetting : list) {
            String date = DateUtil.format(orderSetting.getOrderDate(), "yyyy-MM-dd");

            Integer byDate = ordersettingDao.findByDate(date);
            //日期存在
            if (byDate > 0) {
                ordersettingDao.update(date, orderSetting.getNumber());

            } else {
                //不存在
                ordersettingDao.save(orderSetting);
            }
        }
    }


}
